Facebook PHP SDK V5 integration with Codeigniter 3


Today I will show you the easiest way to integrate Facebook SDK version 5 in your CodeIgniter project. You need to create an app in facebook before using facebook SDK. You can create an app for free easily by visiting following link. The steps are just straight forward and very easy so I am skipping it. Once you create an app you will be provided with an app id and app secret.

Now let's start integrating it into our CodeIgniter project. The easiest way is through composer and if you don't know how to add composer in your CodeIgniter project visit my tutorial The easiest way to add composer into Codeigniter 3

Step one install facebook SDK v5 by the following code composer require "facebook/graph-sdk ~5.0" 

Now in your login view page create a link for facebook login

<p>
    <a href="<?php echo base_url();?>login/fblogin">Login with Facebook</a>
</p>

The codes for fblogin function inside login controller

function fblogin(){

    $fb = new Facebook\Facebook([
          'app_id' => 'your app id',
          'app_secret' => 'your app key',
          'default_graph_version' => 'v2.5',
        ]);

   $helper = $fb->getRedirectLoginHelper();

   $permissions = ['email','user_location','user_birthday','publish_actions']; 
// For more permissions like user location etc you need to send your application for review

   $loginUrl = $helper->getLoginUrl('http://localhost/sampleproject/login/fbcallback', $permissions);

   header("location: ".$loginUrl);
}

The above code will help you to redirect to facebook login page and after a user authenticates and give permission it will redirect to fbcallback function in login controller. The code for it is given below.

function fbcallback(){

        $fb = new Facebook\Facebook([
        'app_id' => 'your app id',
        'app_secret' => 'your app key',
        'default_graph_version' => 'v2.5',
        ]);
        
        $helper = $fb->getRedirectLoginHelper();  
  
        try {  
            
            $accessToken = $helper->getAccessToken();  
            
        }catch(Facebook\Exceptions\FacebookResponseException $e) {  
          // When Graph returns an error  
          echo 'Graph returned an error: ' . $e->getMessage();  
          exit;  
        } catch(Facebook\Exceptions\FacebookSDKException $e) {  
          // When validation fails or other local issues  
          echo 'Facebook SDK returned an error: ' . $e->getMessage();  
          exit;  
        }  
 
 
        try {
          // Get the Facebook\GraphNodes\GraphUser object for the current user.
          // If you provided a 'default_access_token', the '{access-token}' is optional.
          $response = $fb->get('/me?fields=id,name,email,first_name,last_name,birthday,location,gender', $accessToken);
         // print_r($response);
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
          // When Graph returns an error
          echo 'ERROR: Graph ' . $e->getMessage();
          exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
          // When validation fails or other local issues
          echo 'ERROR: validation fails ' . $e->getMessage();
          exit;
        }
    
        // User Information Retrival begins................................................
        $me = $response->getGraphUser();
        
        $location = $me->getProperty('location');
        echo "Full Name: ".$me->getProperty('name')."<br>";
        echo "First Name: ".$me->getProperty('first_name')."<br>";
        echo "Last Name: ".$me->getProperty('last_name')."<br>";
        echo "Gender: ".$me->getProperty('gender')."<br>";
        echo "Email: ".$me->getProperty('email')."<br>";
        echo "location: ".$location['name']."<br>";
        echo "Birthday: ".$me->getProperty('birthday')->format('d/m/Y')."<br>";
        echo "Facebook ID: <a href='https://www.facebook.com/".$me->getProperty('id')."' target='_blank'>".$me->getProperty('id')."</a>"."<br>";
        $profileid = $me->getProperty('id');
        echo "</br><img src='//graph.facebook.com/$profileid/picture?type=large'> ";
        echo "</br></br>Access Token : </br>".$accessToken; 
        
       
    }

 

The above function will fetch basic user information from the facebook and displays it.The sample output is as below.

Facebook sdk sample output

Now for making your own facebook authentication system in your project by steps like storing the user information in the table if new and user session to store necessary data for your authentication and at last redirect to the appropriate page. I just skipped that steps because it mainly depends on your authentication system codes and I don't want to mess it.


Web development
22nd Jul 2018 02:01:45 AM
PHP CodeIgniter
23813

ShareurCodes

ShareurCodes is a code sharing site for programmers to learn, share their knowledge with younger generation.